/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ssau.entities;

import ru.ssau.lib.PresentableAsStringIntegerPair;
import java.io.Serializable;
import org.jdom.Element;
import ru.ssau.lib.StringIntegerPair;
import ru.ssau.lib.XMLizable;
import ru.ssau.utils.XMLHelper;

public class Thread implements Serializable, PresentableAsStringIntegerPair, XMLizable {

    private int id;
    private String caption;

    public Thread(int id, String caption) {
        this.id = id;
        this.caption = caption;
    }

    public Element toXMLElement() {
        Element root = new Element("Thread");
        XMLHelper xhelp = new XMLHelper();
        xhelp.addElement(root, "id", id);
        xhelp.addElement(root, "caption", caption);
        return root;
    }

    public void loadFromXMLElement(Element data) {
        Element temp = null;
        if ((temp = data.getChild("id")) != null) {
            setId(Integer.valueOf(temp.getValue()));
        }
        if ((temp = data.getChild("caption")) != null) {
            setCaption(temp.getValue());
        }
    }

    public Thread() {
        caption = "";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        ru.ssau.entities.Thread other = (ru.ssau.entities.Thread) obj;
        return other.getCaption().equals(caption) && (other.getId() == id);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + this.id;
        hash = 29 * hash + (this.caption != null ? this.caption.hashCode() : 0);
        return hash;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName());
        sb.append(": [id=");
        sb.append(id);
        sb.append(", caption=");
        sb.append(caption);
        sb.append("]");
        return sb.toString();
    }

    public StringIntegerPair toStringIntegerPair() {
        return new StringIntegerPair(id, caption);
    }
}
